package com.servlet;

import com.dao.Impl.ResourceDaoImpl;
import com.dao.Impl.ResourceRoleDaoImpl;
import com.dao.Impl.RoleDaoImpl;
import com.entity.PageBean;
import com.entity.Resource;
import com.entity.ResourceRole;
import com.entity.Role;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐白
 */
@WebServlet(value = "/permissions/*")
public class PermissionsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methon = MyUtil.getMethon(request);
        switch (methon){
            case "show":show(request,response);break;
            case "qxShow": qxShow(request,response);break;
            case "addRole":addrole(request,response);break;
            case "findNameByRole":findNameByRole(request,response);break;
            case "updateRole":updateRole(request,response);break;
            case "deleteRole":deleteRole(request,response);break;
            case "deleteQx":deleteQx(request,response);break;
            default:break;
        }
    }

    private void deleteQx(HttpServletRequest request, HttpServletResponse response) {
        Integer roleId = Integer.valueOf(request.getParameter("roleId"));
        String[] qxIds = request.getParameterMap().get("qxIds[]");
        int i = new ResourceRoleDaoImpl().deleteByRole(roleId);
        if(qxIds!=null){
            for (int j = 0; j < qxIds.length; j++) {
                new ResourceRoleDaoImpl().insertByRole(Integer.parseInt(qxIds[j]),roleId);
            }
        }
        MyUtil.jsonWriter(response,i);
    }

    private void qxShow(HttpServletRequest request, HttpServletResponse response) {
        String roleName = request.getParameter("roleName");
        Role role = new RoleDaoImpl().findByName(roleName);
        List<Integer> id = new ResourceDaoImpl().findByRoleId(role.getId());
        List<Resource> all = new ResourceDaoImpl().findByResource();
        Map map = new HashMap();
        map.put("role",role);
        map.put("all",all);
        map.put("resource",id);
        MyUtil.jsonWriter(response,map);
    }

    private void deleteRole(HttpServletRequest request, HttpServletResponse response) {
        int i = new RoleDaoImpl().deleteRole(request.getParameter("roleName"));
        MyUtil.jsonWriter(response,i);
    }

    private void updateRole(HttpServletRequest request, HttpServletResponse response) {
        Role role = new Role();
        TransBean.populate(role,request.getParameterMap());
        int i = new RoleDaoImpl().updateRole(role);
        MyUtil.jsonWriter(response,i);
    }

    private void findNameByRole(HttpServletRequest request, HttpServletResponse response) {
        Role role = new RoleDaoImpl().findByName(request.getParameter("roleName"));
        MyUtil.jsonWriter(response,role);
    }

    private void addrole(HttpServletRequest request, HttpServletResponse response) {
        Role role = new Role();
        TransBean.populate(role,request.getParameterMap());
        int i = new RoleDaoImpl().addRole(role);
        MyUtil.jsonWriter(response,i);

    }

    private void show(HttpServletRequest request, HttpServletResponse response) {
        PageBean pageBean = new PageBean();
        TransBean.populate(pageBean, request.getParameterMap());
        new RoleDaoImpl().findAllPege(pageBean);
        MyUtil.jsonWriter(response,pageBean);

    }


}
